#region license
# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""The interface to the GUI."""

namespace Grawlix.Application.Gui
import Grawlix
import Grawlix.Meta
import Grawlix.Services.Rendering
import System

[Disposable]
transient class GuiManager:
"""Construct a new GUI handler."""
	
	def Update(elapsed as timespan):
	"""
	Update synchronous GUI stuff.
	Param elapsed: Amount of time since last update.
	"""
		pass
	
	def Render([required] device as IRenderDevice, area as Rect):
	"""
	Render the top level control and subcontrols.
	Param device: The device to use to render.
	Param area: The current viewport.
	"""
		# cache the viewport
		# important to only read from this
		# field during update
		_viewport = area
		
		top = TopLevel
		if top is not null:
			top.Render(device, area)
	
	def FindMouseFocus(ref position as Vector,
	                   ref area as Rect):
	"""
	Find the point of mouse focus, handling mouse enter and
	leave events.
	Param position:
		in: The position of the cursor.
		out: The position of the cursor within the area.
	Param area:
		out: The area of the control.
	Returns: The control that the mouse is over. May be null.
	"""
		if not area.Contains(position):
			return null
		area = Viewport
		control as Control = null
		if TopLevel is not null:
			control = TopLevel.MouseTest(position, area)
			assert area.Contains(position)
		if _lastMouseFocus is not control:
			if _lastMouseFocus is not null:
				_lastMouseFocus.OnMouseLeave(_lastPosition, _lastArea)
			if control is not null:
				control.OnMouseEnter(position, area)
		_lastMouseFocus = control
		_lastPosition = position
		_lastArea = area
		return control
	
	[property(TopLevel)]
	private _topLevel as Control
	"""The top level control currently displayed, if any."""
	
	[getter(Viewport)]
	private _viewport = Rect(-1, -1, 1, 1)
	"""The dimensions of the screen, in pixels."""
	
	[property(KeyboardFocus)]
	private _keyboardFocus as Control
	"""
	The control that receives keyboard events.
	Remarks: Set to null to receive them on the main action.
	"""
	
	[getter(LastMouseFocus, Protected: true)]
	private _lastMouseFocus as Control
	"""The last control to get mouse focus, if any."""
	
	[getter(LastPosition, Protected: true)]
	private _lastPosition = Vector(0, 0)
	"""The last known position of the mouse."""
	
	[getter(LastArea, Protected: true)]
	private _lastArea = Rect(0, 0, 0, 0)
	"""The last area containing the last known position."""
	
	





